<template>
  <div>
    <BreakSpace label="防盗码获取" :containerStyle="{ marginBottom: '15px' }" />
    <FormPanel
      ref="FormPanel"
      :list="formList"
      :formType="type"
      labelWidth="120"
      :isSubmitBtn="false"
      @formChange="formChangeHandle"
    >
    </FormPanel>
    <BaseDialog :visible.sync="visbleVehicle" title="车辆选择"
                :containerStyle="{ overflow: 'auto', paddingBottom: '60px' }"
                destroyOnClose>
      <add-vehicle
        selectionType="single"
        @selectArr="selectVehicleFun"
        @closeDialog='visbleVehicle = false'></add-vehicle>
    </BaseDialog>
    <div style="text-align: center;margin-bottom: 20px;">
      <el-button @click="saveDialog" type="primary" :loading="saveLoading">获 取</el-button>
    </div>
    <div style="text-align: center">
      <FormPanel
        ref="Form"
        :list="formList2"
        labelWidth="120"
        :formType="type"
        :isSubmitBtn="false"
        @formChange="formChangeHandle2"
      >
      </FormPanel>
    </div>
  </div>
</template>

<script>
import {getSecurityCodeByVin,getAuthorizationCode} from '@/api/customerService/SecurityCode/securityCodeUpdate/index';
import { dictionary } from '@/utils/dictMixin';
import {selectByPJSX} from '@/api/customerService/SecurityCode/obtainMode/index';
import { setSccessRecord } from '@/api/customerService/SecurityCode/getSccessRecord/index';
import addVehicle from './Vehicle';

export default {
  name: "getSecurityCode",
  components: {
    addVehicle
  },
  mixins: [dictionary],
  props: {
    type: {
      type: String,
      default: 'add',
    },
  },
  data() {
    this.saveData = null;
    return {
      formList: this.createFormList(),
      formList2: this.createFormList2(),
      activeName: 'first',
      visbleVehicle: false,
      saveLoading: false,
      securityCode: '',
    };
  },
  created() {
    this.init();
  },
  methods: {
    async init() {
    },
    createFormList2() {
      return[
        {
          type: 'INPUT',
          selfCols: 1,
          offsetLeftCols: 1,
          label: '验证码/防盗码',
          fieldName: 'securityCode',
          pinNo: '',
          scNo: '',
          skcNo: '',
          readonly: true,
          placeholder: '',
          initialValue: ''
        }
      ]
    },
    createFormList() {
      return [
        {
          type: 'INPUT',
          label: 'VIN',
          fieldName: 'vin',
          placeholder: '请选择',
          initialValue: '',
          readonly: true,
          rules: [
            {required: true, trigger: 'change', message: '请选择VIN码'}
          ],
          unitRender: () => {
            return (
              <div>
                <el-button icon="el-icon-search" onClick={() => this.visbleVehicle = true}></el-button>
                <el-button slot="append" icon="el-icon-delete" onClick={() => this.clearSelectVin()}></el-button>
              </div>
            );
          }
        },
        {
          type: 'INPUT',
          label: '车型',
          fieldName: 'model',
          readonly: true,
          placeholder: '请选择VIN码',
        },
        {
          type: 'SELECT',
          label: '获取方式',
          fieldName: 'acquisitionMethod',
          readonly: true,
          itemList: this.createDictList('5013'),
          placeholder: '请选择VIN码',
          disabled: true,
          rules: [
            {required: true, trigger: 'change', message: '请选择VIN码'}
          ],
        },
        {
          type: 'INPUT',
          label: '申请码',
          fieldName: 'requestCode',
          rules:[{required: true, message: '必填', trigger: 'change'}],
          hidden: true,
          change:(index,data)=>{
            this.changeRequestCode(index,data)
          }
        },
        {
          type: 'SELECT',
          label: '获取内容',
          fieldName: 'obtainContent',
          itemList: this.createDictList('5015'),
          initialValue: 50151001,
          rules:[{required: true, message: '必填', trigger: 'change'}],
          hidden: true,
          change:(index,data)=>{
            this.changeObtainContent(index,data)
          }
        }
      ]
    },
    clearSelectVin(){ // 清除选择的VIN码
      this.formList.find(item => item.fieldName === 'vin').initialValue = ''
      this.formList.find(item => item.fieldName === 'model').initialValue = ''
      this.formList.find(item => item.fieldName === 'acquisitionMethod').initialValue = ''
    },
    changeRequestCode(index,data){ // 修改申请码
      this.formList2.find(item => item.fieldName === 'securityCode').initialValue = ''
    },
    changeObtainContent(index,data){ // 修改获取内容
      this.formList2.find(item => item.fieldName === 'securityCode').initialValue = ''
    },
    /*选择车辆信息回调 */
    async selectVehicleFun(arr) {
      console.info(arr)
      this.pinNo= arr[0].pinNo;
      this.scNo= arr[0].scNo;
      this.skcNo= arr[0].skcNo;
      this.formList.find(item => item.fieldName === 'model').initialValue= arr[0].modelName;
      this.formList.find(item => item.fieldName === 'vin').initialValue= arr[0].vin;
      this.formList.find(item => item.fieldName === 'requestCode').initialValue = ''
      this.formList.find(item => item.fieldName === 'obtainContent').initialValue = 50151001
      this.formList2.find(item => item.fieldName === 'securityCode').initialValue = ''
      if (arr[0].powerType == 60081001) { //燃油
        const result = await selectByPJSX({attribute: 50001001}) // 燃油防盗码获取方式
        if (result.data.length != 0) {
          this.formList.find(item => item.fieldName === 'acquisitionMethod').initialValue = result.data[0].acquisitionMethod
        }else{
          this.$notify.error({ title: '提示信息', message: '此车辆未维护防盗码获取方式，暂时无法获取防盗码' });
        }
      } else if (arr[0].powerType == 60081002) { //新能源
        const result = await selectByPJSX({attribute: 50001002}) // 新能源防盗码获取方式
        if (result.data.length != 0) {
          this.formList.find(item => item.fieldName === 'acquisitionMethod').initialValue = result.data[0].acquisitionMethod
        }else{
          this.$notify.error({ title: '提示信息', message: '此车辆未维护防盗码获取方式，暂时无法获取防盗码' });
        }
      }else{
        this.$notify.error({ title: '提示信息', message: '此车辆未维护防盗码获取方式，暂时无法获取防盗码' });
      }
      if (this.formList.find(item => item.fieldName === 'acquisitionMethod').initialValue == 50131001){ //直接以VIN获取防盗码
        this.formList.find(item => item.fieldName === 'requestCode').hidden = true
        this.formList.find(item => item.fieldName === 'obtainContent').hidden = false
      } else if(this.formList.find(item => item.fieldName === 'acquisitionMethod').initialValue == 50131002){ // 通过申请码获取授权码
        this.formList.find(item => item.fieldName === 'requestCode').hidden = false
        this.formList.find(item => item.fieldName === 'obtainContent').hidden = true
      }
      this.visbleVehicle = false
    },
    formChangeHandle(val) {
      this.saveData = val;
    },
    formChangeHandle2(val) {
      console.info(val)
    },
    async saveDialog() {
      let data= await this.$refs.FormPanel.SUBMIT_FORM()
      if (data === null) {
        return false
      }
      if (this.formList2.find(item => item.fieldName === 'securityCode').initialValue === ''){ // 已获取防盗码
        this.saveLoading = true
        if (this.formList.find(item => item.fieldName === 'acquisitionMethod').initialValue === 50131001){ // 直接以VIN获取
          if (this.formList.find(item => item.fieldName === 'obtainContent').initialValue === 50151001 && this.scNo != '' && this.scNo != null){//获取sc码
            this.formList2.find(item => item.fieldName === 'securityCode').initialValue = this.scNo
            this.saveData.securityCode = this.scNo
            setSccessRecord(this.saveData)
          } else if(this.formList.find(item => item.fieldName === 'obtainContent').initialValue === 50151002 && this.skcNo != '' && this.skcNo != null) { // 获取skc码
            this.formList2.find(item => item.fieldName === 'securityCode').initialValue = this.skcNo
            this.saveData.securityCode = this.skcNo
            setSccessRecord(this.saveData)
          } else { // 销售车未维护防盗码
            await getSecurityCodeByVin(this.saveData).then(res=>{
              console.info(res)
              if (res.data[0]){
                if (this.formList.find(item => item.fieldName === 'obtainContent').initialValue === 50151001){ //获取sc码
                  this.formList2.find(item => item.fieldName === 'securityCode').initialValue = res.data[0].scCode
                }else{ // 获取skc码
                  this.formList2.find(item => item.fieldName === 'securityCode').initialValue = res.data[0].skcCode
                }
                this.saveData.securityCode = this.formList2.find(item => item.fieldName === 'securityCode').initialValue
                console.info(this.saveData)
                setSccessRecord(this.saveData)
              }else{
                this.$notify.error({ title: '提示信息', message: '该车辆还未维护防盗码！' });
              }
            })
          }
        }else{ // 通过申请码获取
          if (this.pinNo == "" || this.pinNo == null){
            this.$notify.error({ title: '提示信息', message: '此辆车PIN码不存在，无法获取防盗码！' });
            this.saveLoading = false
            return false
          }
          await getAuthorizationCode({strVin: this.saveData.vin,strPwd: this.pinNo}).then(res=>{
            console.info(res)
            this.formList2.find(item => item.fieldName === 'securityCode').initialValue = res.data
          })
          this.saveData.securityCode = this.formList2.find(item => item.fieldName === 'securityCode').initialValue
          setSccessRecord(this.saveData)
          console.info(this.saveData)
        }
        this.saveLoading = false
      }else{
        this.$notify.error({ title: '提示信息', message: '已获取防盗码，请勿重复获取' });
      }
    },
    closeDialog(param) {
      this.$emit('close', false, param);
    }
  },
}
</script>

<style scoped>

</style>
